package com.hotel.service;

import com.hotel.dao.BookingDAO;
import com.hotel.dao.RoomDAO;
import com.hotel.model.Booking;
import com.hotel.model.Room;
import com.hotel.util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class HotelService {
    private RoomDAO roomDAO;
    private BookingDAO bookingDAO;
    
    public HotelService() {
        this.roomDAO = new RoomDAO();
        this.bookingDAO = new BookingDAO();
    }
    
    public List<Room> getAllRooms() {
        return roomDAO.getAllRooms();
    }
    
    public List<Room> getAvailableRooms() {
        return roomDAO.getAvailableRooms();
    }
    
    public Room getRoomById(int roomId) {
        return roomDAO.getRoomById(roomId);
    }
    
    public boolean bookRoom(Booking booking) {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false); // 开启事务
            
            // 检查房间是否可用
            Room room = roomDAO.getRoomById(booking.getRoomId());
            if (room == null || !"available".equals(room.getStatus())) {
                System.err.println("房间不可用: roomId=" + booking.getRoomId());
                conn.rollback();
                return false;
            }
            
            // 添加预订
            boolean bookingAdded = bookingDAO.addBooking(booking);
            if (!bookingAdded) {
                System.err.println("添加预订失败");
                conn.rollback();
                return false;
            }
            
            // 更新房间状态
            boolean roomUpdated = roomDAO.updateRoomStatus(booking.getRoomId(), "occupied");
            if (!roomUpdated) {
                System.err.println("更新房间状态失败");
                conn.rollback();
                return false;
            }
            
            conn.commit(); // 提交事务
            System.out.println("预订成功完成");
            return true;
            
        } catch (SQLException e) {
            System.err.println("预订过程中发生错误: " + e.getMessage());
            e.printStackTrace();
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            return false;
        } finally {
            if (conn != null) {
                try {
                    conn.setAutoCommit(true);
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public List<Booking> getAllBookings() {
        return bookingDAO.getAllBookings();
    }
    
    public boolean confirmBooking(int bookingId) {
        try {
            boolean result = bookingDAO.updateBookingStatus(bookingId, "confirmed");
            System.out.println("确认预订结果: " + result + ", bookingId: " + bookingId);
            return result;
        } catch (Exception e) {
            System.err.println("确认预订失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean cancelBooking(int bookingId, int roomId) {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);
            
            // 获取预订信息验证
            Booking booking = bookingDAO.getBookingById(bookingId);
            if (booking == null) {
                System.err.println("预订不存在: bookingId=" + bookingId);
                conn.rollback();
                return false;
            }
            
            // 取消预订
            boolean bookingCancelled = bookingDAO.updateBookingStatus(bookingId, "cancelled");
            if (!bookingCancelled) {
                System.err.println("取消预订失败: bookingId=" + bookingId);
                conn.rollback();
                return false;
            }
            
            // 释放房间（只有当前状态为occupied时才释放）
            Room room = roomDAO.getRoomById(roomId);
            if (room != null && "occupied".equals(room.getStatus())) {
                boolean roomReleased = roomDAO.updateRoomStatus(roomId, "available");
                if (!roomReleased) {
                    System.err.println("释放房间失败: roomId=" + roomId);
                    conn.rollback();
                    return false;
                }
            }
            
            conn.commit();
            System.out.println("取消预订成功完成");
            return true;
            
        } catch (SQLException e) {
            System.err.println("取消预订过程中发生错误: " + e.getMessage());
            e.printStackTrace();
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            return false;
        } finally {
            if (conn != null) {
                try {
                    conn.setAutoCommit(true);
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
